589. N 叉树的前序遍历
为保证权益,题目请参考 589. N 叉树的前序遍历(From LeetCode).
解决方案1
Python
python
# 589. N 叉树的前序遍历
# https://leetcode-cn.com/problems/n-ary-tree-preorder-traversal/
from typing import List
# Definition for a Node.
class Node:
def __init__(self, val=None, children=None):
self.val = val
self.children = children
class Solution:
def preorder(self, root: 'Node') -> List[int]:
self.a = []
def dfs(root: 'Node'):
if root is not None:
self.a.append(root.val)
for ch in root.children:
dfs(ch)
dfs(root)
return self.a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24